<?php
/**
 * Outputs data to user on request
 *
 *
 **/

// XXX this needs to be replaced
function default_increment() {
 return 10;
}

require 'tpl.php';
require 'thread.php';

if( !is_string($_REQUEST['type']) ) {
  // error BREAK
}

switch( $_REQUEST['type'] ) {
  case 'POST':
          echoPosts(); break;
  case 'THREAD':
          echoThreads(); break;
  case 'FORA':
          echoFora(); break;
  default:
          echoFora(); break;
}

// again, abstraction beckons us
function echoPosts() {
  if( $_REQUEST['tnum'] < 0 ) {
    die( "BREAKAGE" ); // BREAKAGE
  }
  $tnum = (int)$_REQUEST['tnum'];
  $start = (int)$_REQUEST['start'];
  $inc = ((int)$_REQUEST['inc'] > 0) ? (int)$_REQUEST['inc'] : default_increment();

  tplHead();
  tplPostReplyButton( $post );
  // TODO we should really error check getPosts for NULL
  foreach( getPosts( $tnum, $start, $inc ) as $post ) {
    tplPost( $post );
    tplPostEditButton( $post ); 
    tplDeletePostButton( $post['tid'], $post['rank'] );
    // check to see if there is more posts and echo a pager
  }
  tplPostPagerBar($tnum, postCount($tnum), $start, $inc);
  tplPostReplyButton( $post );
  tplFoot();
}
function echoThreads() {
  if( $_REQUEST['fnum'] < 0 ) {
    die( "BREAKAGE" ); // BREAKAGE
  }
  $fnum = (int)$_REQUEST['fnum'];
  $start = (int)$_REQUEST['start'];
  $inc = ((int)$_REQUEST['inc'] > 0) ? (int)$_REQUEST['inc'] : default_increment();

  tplHead();
  tplNewThreadButton( $fnum );
  foreach( getThreads($fnum, $start, $inc) as $thread ) {
    tplThread( $thread );
    tplDeleteThreadButton( $thread['fid'], $thread['id'] );
  }
  tplNewThreadButton( $fnum );
  tplThreadPagerBar($fnum, threadCount($fnum), $start, $inc);
  tplFoot();
}
function echoFora() {

  $start = (int)$_REQUEST['start'];
  $end = ((int)$_REQUEST['inc'] > 0) ? (int)$_REQUEST['inc'] : default_increment();

  tplHead();
  foreach( getFora(null, $start, $end) as $forum ) {
    tplForum( $forum );
  }
  tplForumEditButton();
  tplFoot();
}

?>

